// pages/map/getOrderNotGoing/getOrderNotGoing.js
const handler = require("../../../utils/function.js");
var api = require("../../../api.js"),
    app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
      img: handler.userImg,
        markers: [],
        polyline: [{
            points: [],
            color: "#0299de",
            width: 9,
            dottedLine: false,
            arrowLine: true,
        }],
        controls: [{
            id: 1,
            iconPath: '../../../images/user/icon-22.png',
            position: {
                left: 10,
                top: 10,
                width: 50,
                height: 50
            },
            clickable: true
        }, {
            id: 2,
            iconPath: '../../../images/user/icon-30.png',
            position: {
                top: 280,
                left: 10,
                width: 50,
                height: 50
            },
            clickable: true
        } ], //人物图片定位
        driver: {},
        order_going_s: 0,
        order_going_m: 0,
        startAddress: null,
        myShow: true, //显示my的页面
        zuoyou: "icon-zuoyou", //my
        avatar: "../../../images/user/icon-07.png",
        buttonText: "登录",
        username: "未登录",
        orderGoing: false,
        homeHandlerShow: true,
        phone: "",
        order_no: '',
        is_service: 1,
        initiate:0,//发起收款
        left_width: 750,
        target_adds:'',
    },
    regionchange(e) {
        console.log(e.type)
    },
    markertap(e) {
        console.log(e.markerId)
    },
    // 地图图片点击事件
    controltap(e) {
        console.log("map controltap", e.controlId)
        if (e.controlId === 1) {
            this.openMyPageHandler();
        } else {
            const addresses = handler.getLatLntHandler((res) => {
                //  console.log("一键定位",res);
                this.setData({
                    startAddress: res.startAddress, //开始位置
                })
            });
        }
    },
    // 获取位置路线
    getPolyline: function(location) {
      console.log(location,'121212121211111111111111111')
      wx.setStorageSync("startAddress", { location: { lng: location[1].longitude, lat: location[1].latitude } });
      wx.setStorageSync("endAddress", { location: { lng: location[0].longitude, lat: location[0].latitude } });
        handler.getPolyline((res) => {
            if (res) {
                const polyline = this.data.polyline;
                polyline[0].points = res;
                this.setData({
                    polyline
                });
            }
        })
    },
    // openMyPageHandler 打开我的页面
    openMyPageHandler: function() {
        const {
            pageRight,
            myShow
        } = this.data;
        console.log("openMyPageHandler", pageRight, myShow);
        if (myShow) {
            this.setData({
                pageRight: 620,
                left_width: 130,
                myShow: !myShow,
                controls: [{
                    id: 1,
                    iconPath: '../../../images/user/icon-33.png',
                    position: {
                        top: 144,
                        left: 10,
                        width: 41,
                        height: 41
                    },
                    clickable: true
                }]
            });
        } else {
            this.setData({
                pageRight: 0,
              left_width: 750,
                myShow: !myShow,
                controls: [{
                    id: 1,
                    iconPath: '../../../images/user/icon-22.png',
                    position: {
                        left: 10,
                        top: 10,
                        width: 50,
                        height: 50
                    },
                    clickable: true
                }, {
                    id: 2,
                    iconPath: '../../../images/user/icon-30.png',
                    position: {
                        top: 280,
                        left: 10,
                        width: 50,
                        height: 50
                    },
                    clickable: true
                }, ],
            });
          this.changeIcon()
        }
    },
    // deleteOrderHandler 删除订单事件
    deleteOrderHandler: function() {
        wx.showToast({
            title: '订单正在进行中，无法删除订单',
            icon: "none"
        })
    },
    // onlineServerHandler 在线客服功能
    onlineServerHandler: function(e) {
        console.log(e.currentTarget.dataset.phone)
      handler.callPhoneHandler(e.currentTarget.dataset.phone);
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        console.log('获取订单号',options)
      var that = this;
        this.setData({
            order_no: options.order_no
        })
        const time = setInterval(() => {
          if (that.data.is_service == 2 && that.data.initiate==1) {
                clearInterval(time);
            wx.redirectTo({
              url: '../../pay/paidOrder/paidOrder?order_no=' + that.data.order_no
            })
            } else {
                this.postionTask()
            }
        }, 8000);
    },
    // 修改目的地
  intoPaidHandler:function(e){
    wx.navigateTo({
      url: e.currentTarget.dataset.url,
    })
  },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        var that = this;
        wx.showLoading({
        })
        if (wx.getStorageSync("userMessage") !== "") {
          wx.hideLoading()
            this.setData({
                avatar: wx.getStorageSync("userMessage").avatar,
                username: wx.getStorageSync("userMessage").username,
                buttonText: wx.getStorageSync("userMessage").buttonText,
                phone: wx.getStorageSync("phonep")
            });
        }
      handler.getLatLntHandler(res => {
        that.setData({
          startAddress: res.startAddress, //开始位置
        })
      })
      // 获取坐标位置
      this.changeIcon();
    },
    /*
     * 定时获取订单状态及代驾人员位置
     */
    postionTask: function() {
        var that = this;
        // wx.showLoading({});
        app.request({
            url: api.order.setout,
            method: 'post',
            data: {
                order_no: that.data.order_no,
            },
            success: function(rs) {
              // wx.hideLoading()
              console.log(rs,'11111111111111')
              // debugger
                if (rs.code == 1) {
                    wx.setStorageSync("startAddress", rs.data.slang);
                    wx.setStorageSync("endAddress", rs.data.elang);
                    console.log('jingwei du ',rs.data.s_location)
                    const polyline = that.data.polyline;
                    polyline[0].points = rs.data.s_location;
                    wx.setStorageSync("startAddress", rs.data.slang);
                    wx.setStorageSync("endAddress", rs.data.elang);
                    that.setData({
                        startAddress: rs.data.elang,
                        markers: rs.data.start_location,
                        is_service: rs.data.is_service,
                        initiate: rs.data.initiate,
                        driver:rs.data.diver,
                        target_adds:rs.data.target_adds,
                        serverphone: rs.data.serverphone,
                        signature: rs.data.diver.signature
                    });
                  that.getPolyline(rs.data.s_location);
                }
            }
        })
    },
    // 用户信息事件
    /**loginHandler用户登录页面 */
    loginHandler: function(e) {
        if (this.data.buttonText === "登录") {
            handler.loginHandler((res) => {
                // console.log("用户信息事件",res);
                this.setData({
                    res,
                });
                const that = this;
                wx.showModal({
                    title: '获取手机号码',
                    content: '我们需要获取你的电话号码，一边与和你保持实时的联系，防止你的订单丢失',
                    success: function(res) {
                        if (res.confirm) {
                            handler.intoPageHandler("../../user/getPhone/getPhone");
                        }
                    },
                    fail: function() {
                        wx.showToast({
                            title: '登录失败',
                            icon: "none"
                        })
                    }
                })
            })
        } else {
            this.setData({
                avatar: "../../../images/user/icon-07.png",
                username: "未登录",
                buttonText: "登录",
                phone: ""
            });
        }
    },
    // intoPageHandler 页面跳转
    intoPageHandler: function(e) {
        // console.log(e.currentTarget.dataset.skip);
        handler.intoPageHandler(e.currentTarget.dataset.skip);
    },
    // callPhoneHandler 联系客服
    callPhoneHandler: function() {
        handler.callPhoneHandler(wx.getStorageSync("serverPhone"));
    },
  // 修改用户和一键定位位置
  changeIcon: function () {
    const that = this;
    const { controls } = this.data;
    wx.getSystemInfo({
      success: function success(res) {
        var controlsH = res.windowWidth / 750 * 390;//计算一像素的宽度 res.windowWidth/750
        var map_height = res.windowHeight - controlsH;
        console.log(res, '2222222222')
        controls[1]={
          id: 2,
          iconPath: '../../../images/user/icon-30.png',
          position: {
            top: map_height - 56,
            left: 10,
            width: 50,
            height: 50
          },
          clickable: true};
        that.setData({
          winPath: {
            w: res.windowWidth,
            h: res.windowHeight
          },
          map_height,
          controls,
        });
      }
    });
  },
})